package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.PersonEnrollMapper;
import com.zhijian.medical.entity.po.PersonEnroll;
import com.zhijian.medical.entity.vo.request.PersonEnrollSearchVo;
import com.zhijian.medical.entity.vo.response.PersonEnrollVo;
import com.zhijian.medical.service.PersonEnrollService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-报名业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-10-31 16:46:18
 */
@Slf4j
@Service
public class PersonEnrollServiceImpl implements PersonEnrollService {

    @Autowired
    private PersonEnrollMapper mapper;

    @Override
    public PersonEnroll selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonEnrollVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonEnroll personEnroll) {
        Date now = new Date();
        personEnroll.setCreateTime(now);
        personEnroll.setUpdateTime(now);
        return mapper.insert(personEnroll) > 0 ? personEnroll.getId() : 0L;
    }

    @Override
    public boolean update(PersonEnroll personEnroll) {
        Date now = new Date();
        personEnroll.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personEnroll) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PersonEnroll record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PersonEnrollSearchVo personEnrollSearchVo) {
        return mapper.count(personEnrollSearchVo);
    }

    @Override
    public List<PersonEnrollVo> selectList(PersonEnrollSearchVo personEnrollSearchVo) {
        return mapper.selectList(personEnrollSearchVo);
    }

    @Override
    public Page<PersonEnrollVo> selectPage(PersonEnrollSearchVo personEnrollSearchVo) {
        //先直接查总数
        int count = count(personEnrollSearchVo);
        //分页
        Page<PersonEnrollVo> page = new Page<>(true, count, personEnrollSearchVo.getPageNum(), personEnrollSearchVo.getPageSize());
        personEnrollSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonEnrollVo> responseVos = this.selectList(personEnrollSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PersonEnroll> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}